package com.xt.mvcpilot.data;

import java.io.Serializable;
import java.util.Collection;
import java.util.Date;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.Table;
import javax.persistence.FetchType;
import javax.persistence.OneToOne;
import javax.persistence.JoinColumn;
import javax.persistence.CascadeType;

import org.hibernate.annotations.Cascade;
import org.springframework.stereotype.Repository;

import com.xt.core.data.BasicUser; 

@Repository
@Entity
@org.hibernate.annotations.Entity(
		dynamicInsert=true, //not insert null property 
		dynamicUpdate=true  //not update unchanged property
)
@Table(name = "USER")
public final class User extends BasicUser implements Serializable{

	private static final long serialVersionUID = -858336706087478006L;

	@Column(name = "telephone")
	private String telephone;
	
	@OneToOne (optional=true, cascade=CascadeType.ALL, fetch=FetchType.LAZY)
	@JoinColumn(name="addressid", nullable=true)
	@Cascade(value=org.hibernate.annotations.CascadeType.ALL)
	private Address addressHome; 
	
	@ManyToMany(
		targetEntity=Role.class,
		fetch = FetchType.EAGER,
        cascade={CascadeType.PERSIST}
    )
    @JoinTable(
        name="USER_ROLE",
        joinColumns=@JoinColumn(name="USERID"),
        inverseJoinColumns=@JoinColumn(name="ROLEID")
    )
	private Set<Role> roles;
	
	public User() {}

	
	//getters and setters
	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}


	public Address getAddressHome() {
		return addressHome;
	}


	public void setAddressHome(Address addressHome) {
		this.addressHome = addressHome;
	}


	public Set<Role> getRoles() {
		return roles;
	}


	public void setRoles(Set<Role> roles) {
		this.roles = roles;
	}
	
	public void addRole(Role role){
		roles.add(role);
	}
	
	public void removeRole(Role role){
		roles.remove(role);
	}
}
